﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo;
using StockTrader.BusinessService.DataContract.HnxInfo;
using MySql.Data.MySqlClient;
using System.Data;
using StockTrader.DAL.MySQLHelper;

namespace StockTrader.DAL.MySQL.HnxInfo
{
    public class HnxTopPrice : IHnxTopPrice
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters

        private const string PARM_TRADING_DATE = "@TRADINGDATE";
        private const string PARM_STOCK_ID = "@STOCKID";

        private const string PARM_B_ORD_QTTY_3 = "@BQ3";
        private const string PARM_B_ORD_PRICE_3 = "@BP3";
        private const string PARM_B_ORD_QTTY_2 = "@BQ2";
        private const string PARM_B_ORD_PRICE_2 = "@BP2";
        private const string PARM_B_ORD_QTTY_1 = "@BQ1";
        private const string PARM_B_ORD_PRICE_1 = "@BP1";
        private const string PARM_S_ORD_PRICE_1 = "@SP1";
        private const string PARM_S_ORD_QTTY_1 = "@SQ1";
        private const string PARM_S_ORD_PRICE_2 = "@SP2";
        private const string PARM_S_ORD_QTTY_2 = "@SQ2";
        private const string PARM_S_ORD_PRICE_3 = "@SP3";
        private const string PARM_S_ORD_QTTY_3 = "@SQ3";
        private const string PARM_TIMESTAMP = "@TIMESTAMP";

        #endregion

        #region SQL

        private const string INSERT_HNX_TOPPRICE = "INSERT INTO HNX_TOPPRICE(STOCKID, TRADINGDATE, BQ3,BP3,BQ2,BP2,BQ1,BP1,SP1,SQ1,SP2,SQ2,SP3,SQ3,TIMESTAMP) VALUES (@STOCKID, @TRADINGDATE, @BQ3,@BP3,@BQ2,@BP2,@BQ1,@BP1,@SP1,@SQ1,@SP2,@SQ2,@SP3,@SQ3,@TIMESTAMP) ON DUPLICATE KEY UPDATE STOCKID=@STOCKID, TRADINGDATE=@TRADINGDATE, BQ3=@BQ3,BP3=@BP3,BQ2=@BQ2,BP2=@BP2,BQ1=@BQ1,BP1=@BP1,SP1=@SP1,SQ1=@SQ1,SP2=@SP2,SQ2=@SQ2,SP3=@SP3,SQ3=@SQ3,TIMESTAMP=@TIMESTAMP";


        #endregion



        #region IHnxTopPrice Members


        public List<HnxTopPriceData> getHnxNewTopPrice(decimal timeStamp)
        {
            throw new NotImplementedException();
        }

        public List<HnxTopPriceData> getHnxAllTopPrice()
        {
            throw new NotImplementedException();
        }

        public List<HnxTopPriceData> countHnxTopPrice()
        {
            throw new NotImplementedException();
        }

        public void deleteHnxTopPrice()
        {
            throw new NotImplementedException();
        }

        public void insertHnxNewTopPrice(HnxTopPriceData price)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                                     new MySqlParameter( PARM_STOCK_ID,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_TRADING_DATE,MySqlDbType.DateTime),
                                     new MySqlParameter( PARM_B_ORD_QTTY_3,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_B_ORD_PRICE_3,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_B_ORD_QTTY_2,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_B_ORD_PRICE_2,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_B_ORD_QTTY_1,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_B_ORD_PRICE_1,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_PRICE_1,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_QTTY_1,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_PRICE_2,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_QTTY_2,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_PRICE_3,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_S_ORD_QTTY_3,MySqlDbType.Int32),
                                     new MySqlParameter( PARM_TIMESTAMP,MySqlDbType.Int64)
                };

                parms[0].Value = price.StockId;
                parms[1].Value = price.TradingDate;
                parms[2].Value = price.Bq3;
                parms[3].Value = price.Bp3;
                parms[4].Value = price.Bq2;
                parms[5].Value = price.Bp2;
                parms[6].Value = price.Bq1;
                parms[7].Value = price.Bp1;
                parms[8].Value = price.Sp1;
                parms[9].Value = price.Sq1;
                parms[10].Value = price.Sp2;
                parms[11].Value = price.Sq2;
                parms[12].Value = price.Sp3;
                parms[13].Value = price.Sq3;
                parms[14].Value = price.TimeStamp;

                MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_HNX_TOPPRICE, parms);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
